package ZorkXMLParser;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.util.HashMap;

public class ZorkXMLParser
{
    private SAXParserFactory factory;
    private SAXParser saxParser;
    private ZorkXMLHandler handler;

    public ZorkXMLParser()
    {
        try 
        {
            factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();
            handler = new ZorkXMLHandler();
        } 
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public HashMap parse(String XMLFileName)
    {
        HashMap parsedConfigFile = null;

        try 
        {
            saxParser.parse(XMLFileName, handler);
            parsedConfigFile = handler.getMap();
        } 
        catch(java.io.FileNotFoundException e) {
            System.out.println(String.format("%s does not exist.", XMLFileName));
        } 
        catch(Exception e) {
            e.printStackTrace();
        } 
        finally {
            return parsedConfigFile;
        }
    }
}
